package com.yangwei.examination.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yangwei.examination.student.pojo.AnswerPaper;
import com.yangwei.examination.student.pojo.AnswerPaperChoice;
import com.yangwei.examination.student.pojo.AnswerPaperData;
import com.yangwei.examination.student.pojo.AnswerPaperQuestion;
import com.yangwei.examination.student.service.AnswerPaperChoiceService;
import com.yangwei.examination.student.service.AnswerPaperQuestionService;
import com.yangwei.examination.student.service.AnswerPaperService;
import com.yangwei.examination.teacher.pojo.*;
import com.yangwei.examination.teacher.service.*;
import com.yangwei.examination.user.pojo.Student;
import com.yangwei.examination.user.pojo.User;
import com.yangwei.examination.user.service.TeacherService;


import com.yangwei.examination.utils.ResultUtils;
import com.yangwei.examination.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Api(tags = "学生操作答卷信息",description = "学生&提交答卷&答卷列表&预览答卷")
@RestController
@RequestMapping("/stu/AnswerPaper")
public class AnswerPaperStuController {

    @Resource
    private PaperService paperService;

    @Resource
    private PaperQuestionService paperQuestionService;

    @Resource
    private PaperQuestionChoiceService paperQuestionChoiceService;

    @Resource
    private PaperQuestionAnswerService paperQuestionAnswerService;

    @Resource
    private AnswerPaperService answerPaperService;

    @Resource
    private AnswerPaperQuestionService answerPaperQuestionService;

    @Resource
    private AnswerPaperChoiceService answerPaperChoiceService;

    @Resource
    private ExamService examService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private CorrectAnswerPaperService correctAnswerPaperService;

    @Resource
    private CorrectAnswerPaperScoreService correctAnswerPaperScoreService;

    @Resource
    private CorrectAnswerPaperInfoService correctAnswerPaperInfoService;


    //保存答卷
    @ApiOperation(value = "提交答卷信息",notes = "返回值未完善")
    @PostMapping("/submit")
    @Transactional
    public ResultVo saveAnswerPaper(@RequestBody AnswerPaperData answerPaperData) {

        //保存AnswerPaper
        AnswerPaper answerPaper = new AnswerPaper();
        //学生id 测试id 试卷id
        answerPaper.setPaperId(answerPaperData.getPaperData().getId());
        answerPaper.setExamId(answerPaperData.getExamData().getId());
        answerPaper.setStudentId(answerPaperData.getUser().getId());
        boolean save = answerPaperService.save(answerPaper);

        //获取自增id
        Long answerPaperId = null;
        if (save) {
            answerPaperId = answerPaper.getId();
        }

        //将内容填入answerPaperChoiceList便于saveBatch
        List<AnswerPaperChoice> answerPaperChoiceList = new ArrayList<>();

        //单选
        List<PaperQuestion> singleList = answerPaperData.getSingleList();
        for (PaperQuestion paperQuestion : singleList) {

            AnswerPaperQuestion answerPaperQuestion = new AnswerPaperQuestion();
            answerPaperQuestion.setAnswerPaperId(answerPaperId);
            answerPaperQuestion.setQuestionId(paperQuestion.getId());
            answerPaperQuestion.setQuestionType("1");
            answerPaperQuestionService.save(answerPaperQuestion);

            AnswerPaperChoice answerPaperChoice = new AnswerPaperChoice();
            answerPaperChoice.setAnswerPaperQuestionId(answerPaperQuestion.getId());
            answerPaperChoice.setAnswerValue(paperQuestion.getSelected());
            answerPaperChoiceList.add(answerPaperChoice);

        }
        //多选
        List<PaperQuestion> multiList = answerPaperData.getMultiList();
        for (PaperQuestion paperQuestion : multiList) {
            AnswerPaperQuestion answerPaperQuestion = new AnswerPaperQuestion();
            answerPaperQuestion.setAnswerPaperId(answerPaperId);
            answerPaperQuestion.setQuestionId(paperQuestion.getId());
            answerPaperQuestion.setQuestionType("2");
            answerPaperQuestionService.save(answerPaperQuestion);

            AnswerPaperChoice answerPaperChoice = new AnswerPaperChoice();
            answerPaperChoice.setAnswerPaperQuestionId(answerPaperQuestion.getId());
            answerPaperChoice.setAnswerValue(paperQuestion.getSelected());
            answerPaperChoiceList.add(answerPaperChoice);
        }
        //填空
        List<PaperQuestion> fillInList = answerPaperData.getFillInList();
        for (PaperQuestion paperQuestion : fillInList) {
            AnswerPaperQuestion answerPaperQuestion = new AnswerPaperQuestion();
            answerPaperQuestion.setAnswerPaperId(answerPaperId);
            answerPaperQuestion.setQuestionId(paperQuestion.getId());
            answerPaperQuestion.setQuestionType("3");
            answerPaperQuestionService.save(answerPaperQuestion);

            AnswerPaperChoice answerPaperChoice = new AnswerPaperChoice();
            answerPaperChoice.setAnswerPaperQuestionId(answerPaperQuestion.getId());
            answerPaperChoice.setAnswerValue(paperQuestion.getSelected());
            answerPaperChoiceList.add(answerPaperChoice);
        }
        //简答
        List<PaperQuestion> textList = answerPaperData.getTextList();
        for (PaperQuestion paperQuestion : textList) {
            AnswerPaperQuestion answerPaperQuestion = new AnswerPaperQuestion();
            answerPaperQuestion.setAnswerPaperId(answerPaperId);
            answerPaperQuestion.setQuestionId(paperQuestion.getId());
            answerPaperQuestion.setQuestionType("4");
            answerPaperQuestionService.save(answerPaperQuestion);

            AnswerPaperChoice answerPaperChoice = new AnswerPaperChoice();
            answerPaperChoice.setAnswerPaperQuestionId(answerPaperQuestion.getId());
            answerPaperChoice.setAnswerValue(paperQuestion.getSelected());
            answerPaperChoiceList.add(answerPaperChoice);
        }
        answerPaperChoiceService.saveBatch(answerPaperChoiceList);

        return ResultUtils.success("提交成功！", null);
    }

    //查答卷List
    @ApiOperation(value = "查本人答卷列表")
    @GetMapping("/list")
    public ResultVo list(@RequestParam Integer studentId) {

        List<AnswerPaper> list = answerPaperService.list(Wrappers.lambdaQuery(AnswerPaper.class).eq(AnswerPaper::getStudentId, studentId).orderByDesc(AnswerPaper::getId));
        for (AnswerPaper answerPaper : list) {
            answerPaper.setExam(examService.getById(answerPaper.getExamId()));
            answerPaper.getExam().setTeacher(teacherService.getById(answerPaper.getExam().getTeacherId()));
            answerPaper.getExam().setPaper(paperService.getById(answerPaper.getExam().getPaperId()));
            CorrectAnswerPaper one = correctAnswerPaperService.getOne(Wrappers.lambdaQuery(CorrectAnswerPaper.class)
                    .eq(CorrectAnswerPaper::getAnswerPaperId, answerPaper.getId())
            );
            if (one!=null)
                answerPaper.setIsCorrect(1);
        }

        return ResultUtils.success("查询成功！", list);
    }

    //预览本人答卷
    @ApiOperation(value = "预览本人答卷")
    @PostMapping("/getAnswerPaperView")
    public ResultVo getAnswerPaperView(@RequestBody AnswerPaper answerPaper) {
        System.out.println(answerPaper);
        //查是否被批改过
        boolean isCorrect = false;
        CorrectAnswerPaper one = correctAnswerPaperService.getOne(Wrappers.lambdaQuery(CorrectAnswerPaper.class)
                .eq(CorrectAnswerPaper::getAnswerPaperId, answerPaper.getId())
        );
        Long correctAnswerPaperId=null;
        if (one != null) {
            isCorrect = true;
            correctAnswerPaperId=one.getId();
        }

        //查答卷信息
        List<AnswerPaperQuestion> answerPaperQuestionList = answerPaperQuestionService.list(
                Wrappers.lambdaQuery(AnswerPaperQuestion.class).
                        eq(AnswerPaperQuestion::getAnswerPaperId, answerPaper.getId())
        );
        answerPaper.setAnswerPaperQuestionList(answerPaperQuestionList);

        for (AnswerPaperQuestion answerPaperQuestion : answerPaperQuestionList) {
            answerPaperQuestion.setAnswerPaperChoice(
                    answerPaperChoiceService.getOne(Wrappers.lambdaQuery(AnswerPaperChoice.class).
                            eq(AnswerPaperChoice::getAnswerPaperQuestionId, answerPaperQuestion.getId()))
            );
        }


        //查试卷信息
        Paper paper = paperService.getById(answerPaper.getPaperId());
        if (isCorrect) {
            //注入改卷老师
            Long teacherId = correctAnswerPaperService.getOne(Wrappers.lambdaQuery(CorrectAnswerPaper.class)
                    .eq(CorrectAnswerPaper::getAnswerPaperId, answerPaper.getId())
            ).getTeacherId();
            //注入总得分信息 按照题目类型排序
            List<CorrectAnswerPaperInfo> correctAnswerPaperInfoList = correctAnswerPaperInfoService.list(Wrappers.lambdaQuery(CorrectAnswerPaperInfo.class)
                    .eq(CorrectAnswerPaperInfo::getCorrectAnswerPaperId, one.getId())
                    .orderByAsc(CorrectAnswerPaperInfo::getQuestionType)
            );
            paper.setCorrectAnswerPaperInfo(correctAnswerPaperInfoList);
            paper.setTeacher(teacherService.getById(teacherId));

        }

        List<PaperQuestion> paperQuestionList = paperQuestionService.list(
                Wrappers.lambdaQuery(PaperQuestion.class).
                        eq(PaperQuestion::getPaperId, paper.getId())
        );
        for (PaperQuestion paperQuestion : paperQuestionList) {
            //注入Choice
            List<PaperQuestionChoice> paperQuestionChoiceList = paperQuestionChoiceService.list(
                    Wrappers.lambdaQuery(PaperQuestionChoice.class)
                            .eq(PaperQuestionChoice::getQuestionId, paperQuestion.getId())
                            .orderByAsc(PaperQuestionChoice::getChoiceOrder)
            );
            paperQuestion.setPaperQuestionChoiceList(paperQuestionChoiceList);
            //注入Answer
            List<PaperQuestionAnswer> paperQuestionAnswerList = paperQuestionAnswerService.list(
                    Wrappers.lambdaQuery(PaperQuestionAnswer.class)
                            .eq(PaperQuestionAnswer::getQuestionId, paperQuestion.getId())
                            .orderByAsc(PaperQuestionAnswer::getAnswerOrder)
            );
            paperQuestion.setPaperQuestionAnswerList(paperQuestionAnswerList);
            if (isCorrect) {
                if (paperQuestion.getQuestionType().equals("3") || paperQuestion.getQuestionType().equals("4"))
                    paperQuestion.setScore(
                            correctAnswerPaperScoreService.getOne(Wrappers.lambdaQuery(CorrectAnswerPaperScore.class)
                                    .eq(CorrectAnswerPaperScore::getPaperQuestionId, paperQuestion.getId())
                                    .eq(CorrectAnswerPaperScore::getCorrectAnswerPaperId,correctAnswerPaperId)
                            ).getScore()
                    );
            }
        }
        paper.setPaperQuestionList(paperQuestionList);
        //注入试卷信息
        PaperInfo paperInfo = new PaperInfo();
        for (int i = 1; i <= 4; i++) {
            QueryWrapper<PaperQuestion> paperQuestionQueryCount = new QueryWrapper<>();
            paperQuestionQueryCount.lambda().eq(PaperQuestion::getQuestionType, i).eq(PaperQuestion::getPaperId, paper.getId());
            if (i == 1)
                paperInfo.setSingleNum(paperQuestionService.count(paperQuestionQueryCount));
            if (i == 2)
                paperInfo.setMultipleNum(paperQuestionService.count(paperQuestionQueryCount));
            if (i == 3)
                paperInfo.setFillInNum(paperQuestionService.count(paperQuestionQueryCount));
            if (i == 4)
                paperInfo.setTextNum(paperQuestionService.count(paperQuestionQueryCount));
        }
        paper.setPaperInfo(paperInfo);

        //将选项答案保存进selected
        for (PaperQuestion paperQuestion : paper.getPaperQuestionList()) {
            for (AnswerPaperQuestion answerPaperQuestion : answerPaper.getAnswerPaperQuestionList()) {
                if (paperQuestion.getId().equals(answerPaperQuestion.getQuestionId())) {
                    paperQuestion.setSelected(answerPaperQuestion.getAnswerPaperChoice().getAnswerValue());
                }
            }
        }


        return ResultUtils.success("查询成功！", paper);
    }



}
